package com.lzy.libreoffice.autoconfiguration;

import com.lzy.libreoffice.autoconfiguration.service.LibreofficeService;
import com.lzy.libreoffice.autoconfiguration.service.impl.LibreofficeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(LibreofficeProperties.class)
public class LibreofficeConfiguration {

    @Autowired
    private LibreofficeProperties libreofficeProperties;

    // 在Spring上下文中创建一个对象
    @Bean
    //libreoffice.enabled=true时才创建对象
    @ConditionalOnProperty(prefix = "libreoffice", name = "enabled", havingValue = "true")
    @ConditionalOnMissingBean(LibreofficeService.class)
    public LibreofficeService getLibreofficeService(){
        LibreofficeServiceImpl libreofficeService = new LibreofficeServiceImpl();
        libreofficeService.startLibreOffice(libreofficeProperties.getPath(),libreofficeProperties.getPort());
        return libreofficeService;
    }
}
